/*
 * ATKPropertyEditorFactory.java
 *
 * Created on 22 juni 2007, 10:55
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.jwtools.tmt.ui.propertyeditor;

import java.awt.Color;
import java.beans.PropertyEditor;
import java.util.Date;
import java.util.List;

import com.l2fprod.common.beans.editor.BooleanAsCheckBoxPropertyEditor;
import com.l2fprod.common.beans.editor.ColorPropertyEditor;
import com.l2fprod.common.beans.editor.ComboBoxPropertyEditor;
import com.l2fprod.common.beans.editor.IntegerPropertyEditor;
import com.l2fprod.common.beans.editor.JCalendarDatePropertyEditor;
import com.l2fprod.common.beans.editor.LongPropertyEditor;
import com.l2fprod.common.beans.editor.StringPropertyEditor;
import com.l2fprod.common.propertysheet.Property;
import com.l2fprod.common.propertysheet.PropertyEditorFactory;

/**
 * 
 * @author mesui00
 */
public class BasePropertyEditorFactory implements PropertyEditorFactory {

	/** Creates a new instance of ATKPropertyEditorFactory */
	public BasePropertyEditorFactory() {
	}

	public PropertyEditor createPropertyEditor(Property p) {
		ExtendedProperty property = (ExtendedProperty) p;
		String pName = property.getName();

		// List field
		if (isList(property.getDisplayType())) {
			ComboBoxPropertyEditor cbEd = new ComboBoxPropertyEditor();
			cbEd.setAvailableValues(property.getAllowedvalues());
			return cbEd;
		}

		// Date field
		if (property.getDisplayType().equals(Date.class)) {
			// NachoCalendarDatePropertyEditor ed = new
			// NachoCalendarDatePropertyEditor();
			JCalendarDatePropertyEditor ed = new JCalendarDatePropertyEditor();
			ed.setDateFormatString("dd-MM-yyyy");
			return ed;
		}

		// Integer field
		if (property.getDisplayType().equals(Integer.class)) {
			return new IntegerPropertyEditor();
		}

		// Long field
		if (property.getDisplayType().equals(Long.class)) {
			return new LongPropertyEditor();
		}

		// Boolean field
		if (property.getDisplayType().equals(Boolean.class)) {
			return new BooleanAsCheckBoxPropertyEditor();
		}

		// Color field
		if (property.getDisplayType().equals(Color.class)) {
			return new ColorPropertyEditor();
		}

		return new StringPropertyEditor();
	}

	private boolean isList(Class aClass) {
		Class[] ifaces = aClass.getInterfaces();
		if (ifaces == null)
			return false;
		for (int idx = 0; idx < ifaces.length; idx++)
			if (ifaces[idx].equals(List.class))
				return true;
		return false;
	}

}
